


class Food {
}
class Fruit extends Food {
}
class Apple extends Fruit {
}
class Banana extends Fruit {
}


class Plate<T> { // 设置泛型
    private T message;
    public T getMessage() {
        return message;
    }
    public void setMessage(T message) {
        this.message = message;
    }
}


public class Test2 {
    public static void main(String[] args) {
        Plate<Fruit> plate = new Plate<>();
        plate.setMessage(new Fruit());
        fun2(plate);

        Plate<Food> plate2 = new Plate<>();
        plate2.setMessage(new Food());
        fun2(plate2);
    }
    public static void fun2(Plate<? super Fruit> temp){
        // 此时可以修改！！添加的是Fruit 或者Fruit的子类
        temp.setMessage(new Apple());//这个是Fruit的子类
        temp.setMessage(new Fruit());//这个是Fruit的本身
        //Fruit fruit = temp.getMessage(); 不能接收，这里无法确定是哪个父类
        //Fruit fruit = (Fruit)temp.getMessage(); 不能接收，这里无法确定是哪个父类
        System.out.println(temp.getMessage());//只能直接输出
    }



    public static void main1(String[] args) {
        Plate<Apple> plate = new Plate<>();
        plate.setMessage(new Apple());
        fun(plate);

        Plate<Banana> plate2 = new Plate<>();
        plate2.setMessage(new Banana());
        fun(plate2);
    }

    public static void fun(Plate<? extends Fruit> temp){
        //temp.setMessage(new Banana());//无法修改
        //temp.setMessage(new Apple());//无法修改

        Fruit fruit = temp.getMessage();//向上转型
        System.out.println(fruit);
    }
}
